package cz.muni.fi.pa036.kiss.serviceimpl;

import cz.muni.fi.pa036.kiss.Entity.Rezervace;
import cz.muni.fi.pa036.kiss.Entity.RezervaceAssembler;
import cz.muni.fi.pa036.kiss.Entity.RezervaceDTO;
import cz.muni.fi.pa036.kiss.Entity.SluzbaKolejeAssembler;
import cz.muni.fi.pa036.kiss.Entity.SluzbaKolejeDTO;
import cz.muni.fi.pa036.kiss.Entity.StudentAssembler;
import cz.muni.fi.pa036.kiss.Entity.StudentDTO;
import cz.muni.fi.pa036.kiss.dao.RezervaceDAO;
import cz.muni.fi.pa036.kiss.service.RezervaceService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class RezervaceServiceImpl implements RezervaceService {

    @Autowired
    private RezervaceDAO rezervaceDAO;
    @Autowired
    private RezervaceAssembler assembler;
    @Autowired
    private SluzbaKolejeAssembler sluzbaAss;
    @Autowired
    private StudentAssembler studentAss;

    @Transactional
    @Override
    public void createRezervace(RezervaceDTO rezervace) {
        if (rezervace == null) {
            throw new IllegalArgumentException("rezervace is null: ");
        }
        if (rezervace.getId() != null) {
            throw new IllegalArgumentException("id is not null");
        }
        Rezervace rezerv = assembler.createEntityfromDTO(rezervace);
        rezervaceDAO.createRezervace(rezerv);
        rezervace.setId(rezerv.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public RezervaceDTO getRezervace(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        RezervaceDTO rezervace = assembler.createDTOfromEntity(rezervaceDAO.getRezervace(id));
        if (rezervace == null) {
            return null;
        } else {
            return rezervace;
        }
    }

    @Transactional
    @Override
    public void updateRezervace(RezervaceDTO rezervace) {
        if (rezervace == null) {
            throw new IllegalArgumentException("rezervace is null");
        }
        rezervaceDAO.updateRezervace(assembler.createEntityfromDTO(rezervace));
    }

    @Transactional
    @Override
    public void removeRezervace(RezervaceDTO rezervace) {
        if (rezervace == null) {
            throw new IllegalArgumentException("rezervace is null");
        }
        rezervaceDAO.removeRezervace(assembler.createEntityfromDTO(rezervace));
    }

    @Transactional(readOnly = true)
    @Override
    public List<RezervaceDTO> findAll() {
        List<Rezervace> all = rezervaceDAO.findAll();
        List<RezervaceDTO> allDto = new ArrayList<RezervaceDTO>();
        for (Rezervace rezervace : all) {
            allDto.add(assembler.createDTOfromEntity(rezervace));
        }
        return allDto;
    }

    @Transactional(readOnly = true)
    @Override
    public List<RezervaceDTO> findByStudent(StudentDTO student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        List<Rezervace> all = rezervaceDAO.findByStudent(studentAss.createEntityfromDTO(student));
        List<RezervaceDTO> allDto = new ArrayList<RezervaceDTO>();
        for (Rezervace rezervace : all) {
            allDto.add(assembler.createDTOfromEntity(rezervace));
        }
        return allDto;
    }

    @Transactional(readOnly = true)
    @Override
    public List<RezervaceDTO> findBySluzba(SluzbaKolejeDTO sluzbaKoleje) {
        if (sluzbaKoleje == null) {
            throw new IllegalArgumentException("sluzba koleje is null");
        }
        List<Rezervace> all = rezervaceDAO.findBySluzba(sluzbaAss.createEntityfromDTO(sluzbaKoleje));
        List<RezervaceDTO> allDto = new ArrayList<RezervaceDTO>();
        for (Rezervace rezervace : all) {
            allDto.add(assembler.createDTOfromEntity(rezervace));
        }
        return allDto;
    }
}
